package com.prs.crm.action.cqunicom;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.cqunicom.HighGradeExpected;
import com.prs.crm.domain.cqunicom.LogSerch;
import com.prs.crm.domain.cqunicom.PoplarOrder;
import com.prs.crm.domain.cqunicom.Services;
import com.prs.crm.service.cqunicom.HighGradeExpectedService;
import com.prs.crm.service.cqunicom.LogSerchService;
import com.prs.crm.service.cqunicom.OrderService;
import com.prs.crm.service.cqunicom.ServicesService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-3-23 下午01:55:19 简单说明
 */

public class HighGradeAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private PaginationSupport pagination;
	private LogSerchService logSerchService;
	private  HighGradeExpected highGradeExpected;

	private  HighGradeExpectedService highGradeExpectedService;
	
	private Services services;

	private ServicesService servicesService;

	private List<Services> sersList;
	
	private PoplarOrder order;

	private OrderService orderService;
	
	private List<HighGradeExpected>  hList=new ArrayList<HighGradeExpected>();
	
	private List<String> alist=new ArrayList<String>();
	
	private List<String> blist=new ArrayList<String>();
	
	private Date startTime=null;
	
	private Date endTime=null;
	
	private String rowdata;
	
	private Integer hightId=0;
	
	// 时间
	private String starts = "";
	private String ends = "";
	//字段过滤掉
	private String orA=null;
	private String orB=null;
	private String orC=null;
	private String orD=null;
	private String orE=null;
	private String orF=null;
	private String orG=null;
	private String orH=null;
	private String orI=null;
	/**
	 * 起始查询属性
	 */
	private String  initV=null;

	/**
	 * 编辑预定义
	 * 
	 * @return
	 */
	@Action("editExpected")
	public String editExpected(){
		sersList=this.getServicesService().getAllServices();	
		return SUCCESS;
	}
	/**
	 * 查询编辑预定义
	 * 
	 * @return
	 */
	@Action("listExpected")
	public String listExpected(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				HighGradeExpected.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getHighGradeExpectedService().listByPage(criteria,
				pagination));	
		return SUCCESS;
	}
	/**
	 * 查询编辑预定义变更
	 * 
	 * @return
	 */
	@Action("lookHighGradeExpected")
	public String lookHighGradeExpected(){
		sersList=this.getServicesService().getAllServices();	
		HighGradeExpected  he=this.getHighGradeExpectedService().get(highGradeExpected.getId());
		String aa=he.getAdresss();
		if(aa==null || "".equals(aa)){
			
		}else{
		String a[]=aa.split(",");
		for(String as:a){
			alist.add(as);
		}
		}
		String bb=he.getRowName();
      if(bb==null || "".equals(bb)){
			
		}else{
		String a[]=bb.split(",");
		for(String as:a){
			blist.add(as);
		}
		}
		
		
		return SUCCESS;
	}
	
	/**
	 * 预定义查询
	 * 
	 * @return
	 */
	@Action("selectExpected")
	public String selectExpected() {
		SimpleDateFormat ddf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (startTime== null) {
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DAY_OF_MONTH, -7);
			Date _currt = c.getTime();
			starts = ddf.format(_currt);// 开始时间
		} else {
			starts = ddf.format(startTime);
		}
		if (endTime == null) {
			ends = ddf.format(new Date());// 结束时间
		} else {
			ends = ddf.format(endTime);
		}

		hList=this.getHighGradeExpectedService().getAllHighGradeExpected();
		//System.out.println();
		//查询条件过滤
		if("".equals(initV)|| initV==null){
			
		}else{
			HighGradeExpected gO=this.getHighGradeExpectedService().get(hightId);
			//字段过滤处理
			String fileds[]=gO.getRowName().split(",");
			for(String _v:fileds){
				if("订单号".equals(_v)){
					orA="cc";
				}
				else if("客户名称".equals(_v)){
					orB="x";
				}
                else if("客户电话".equals(_v)){
                	orC="x";
				}
                else if("交易状态".equals(_v)){
                	orD="x";
				}
                else if("成家价格".equals(_v)){
                	orE="x";
				}
               else if("下单时间".equals(_v)){
            	   orF="x";
				}
                else if("公司名称".equals(_v)){
                	orG="x";
				}
               else if("回访状态".equals(_v)){
            	   orH="x";
				}
                else if("满意度".equals(_v)){
                	orI="x";
				}
			}
			
			DetachedCriteria criteria = this.getPaginationHelper().createQuery(
					PoplarOrder.class);
			if(gO.getServicesHigh()!=null){
				criteria.createAlias("services", "ser");
				criteria.add(Restrictions.eq("ser.id", gO.getServicesHigh().getId()));
			}
			if(gO.getAdresss()!=null){
				String adressss[]=(gO.getAdresss()).split(",");
				List<String>  ds=new ArrayList<String>();
				for(String ss:adressss){
					ds.add(ss);
				}
				if(ds.size()==0){
					criteria.add(Restrictions.eq("cusaddess", "0"));	
				}else{
				criteria.add(Restrictions.in("cusaddess", ds));	
				}
			}
			//时间startTime=null;
			
			if(endTime!=null && startTime!=null){
				criteria.add(Restrictions.between("orderDate", startTime, endTime));	
			}
			
			
			pagination = this.getPaginationHelper().prepare(pagination);
			this.setPagination(this.getOrderService().listByPage(criteria,
					pagination));	
		}
		return SUCCESS;
	}
    /**
     * 查询说明
     * @return
     */
	
	@Action(value = "serchContents", results = { @Result(name = "success", type = "json", params = {
			"root", "rowdata" }) })
	public String serchContents() {
		HighGradeExpected hg=this.getHighGradeExpectedService().get(hightId);
		
		this.rowdata = hg.getRemark();
		return SUCCESS;
	}

	
	@Clew(results = { "success", "none" })
	@Action(value = "saveGrade", results = {
			@Result(name = "success", type = "redirect", location = "editExpected"),
			@Result(name = "none", type = "redirect", location = "editExpected"),
			@Result(name = "error", type = "chain", location = "editExpected") })
	public String saveGrade() {
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		log.setCandleContents("添加了高级设置");
		this.getLogSerchService().save(log);
		
		this.getHighGradeExpectedService().save(highGradeExpected);
		this.setPrompt("数据处理中.........");
		return SUCCESS;
		
	}
	/**
	 * 变更
	 * @return
	 */
	
	@Clew(results = { "success", "none" })
	@Action(value = "updaGrade", results = {
			@Result(name = "success", type = "redirect", location = "editExpected"),
			@Result(name = "none", type = "redirect", location = "editExpected"),
			@Result(name = "error", type = "chain", location = "editExpected") })
	public String updaGrade() {
		HighGradeExpected  hd=this.getHighGradeExpectedService().get(highGradeExpected.getId());
		hd.setAdresss(highGradeExpected.getAdresss());
		hd.setRemark(highGradeExpected.getRemark());
		hd.setRoless(highGradeExpected.getRoless());
		hd.setRowName(highGradeExpected.getRowName());
		hd.setSerchName(highGradeExpected.getSerchName());
		hd.setServicesHigh(highGradeExpected.getServicesHigh());		
		this.getHighGradeExpectedService().submit(hd);
		this.setPrompt("数据处理中.........");
		return SUCCESS;
		
	}
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Services getServices() {
		return services;
	}

	public PoplarOrder getOrder() {
		return order;
	}
	public void setOrder(PoplarOrder order) {
		this.order = order;
	}
	public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	public List<HighGradeExpected> gethList() {
		return hList;
	}
	public void sethList(List<HighGradeExpected> hList) {
		this.hList = hList;
	}
	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	public String getStarts() {
		return starts;
	}
	public void setStarts(String starts) {
		this.starts = starts;
	}
	public String getEnds() {
		return ends;
	}
	public void setEnds(String ends) {
		this.ends = ends;
	}
	public String getInitV() {
		return initV;
	}
	public void setInitV(String initV) {
		this.initV = initV;
	}
	public HighGradeExpected getHighGradeExpected() {
		return highGradeExpected;
	}

	public List<String> getAlist() {
		return alist;
	}
	public void setAlist(List<String> alist) {
		this.alist = alist;
	}
	public List<String> getBlist() {
		return blist;
	}
	public void setBlist(List<String> blist) {
		this.blist = blist;
	}
	public void setHighGradeExpected(HighGradeExpected highGradeExpected) {
		this.highGradeExpected = highGradeExpected;
	}

	public HighGradeExpectedService getHighGradeExpectedService() {
		return highGradeExpectedService;
	}

	public void setHighGradeExpectedService(
			HighGradeExpectedService highGradeExpectedService) {
		this.highGradeExpectedService = highGradeExpectedService;
	}

	public void setServices(Services services) {
		this.services = services;
	}

	public ServicesService getServicesService() {
		return servicesService;
	}

	public void setServicesService(ServicesService servicesService) {
		this.servicesService = servicesService;
	}

	public List<Services> getSersList() {
		return sersList;
	}

	public void setSersList(List<Services> sersList) {
		this.sersList = sersList;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public String getRowdata() {
		return rowdata;
	}
	public void setRowdata(String rowdata) {
		this.rowdata = rowdata;
	}
	public Integer getHightId() {
		return hightId;
	}
	public void setHightId(Integer hightId) {
		this.hightId = hightId;
	}
	public String getOrA() {
		return orA;
	}
	public void setOrA(String orA) {
		this.orA = orA;
	}
	public String getOrB() {
		return orB;
	}
	public void setOrB(String orB) {
		this.orB = orB;
	}
	public String getOrC() {
		return orC;
	}
	public void setOrC(String orC) {
		this.orC = orC;
	}
	public String getOrD() {
		return orD;
	}
	public void setOrD(String orD) {
		this.orD = orD;
	}
	public String getOrE() {
		return orE;
	}
	public void setOrE(String orE) {
		this.orE = orE;
	}
	public String getOrF() {
		return orF;
	}
	public void setOrF(String orF) {
		this.orF = orF;
	}
	public String getOrG() {
		return orG;
	}
	public void setOrG(String orG) {
		this.orG = orG;
	}
	public String getOrH() {
		return orH;
	}
	public void setOrH(String orH) {
		this.orH = orH;
	}
	public String getOrI() {
		return orI;
	}
	public void setOrI(String orI) {
		this.orI = orI;
	}
	public LogSerchService getLogSerchService() {
		return logSerchService;
	}
	public void setLogSerchService(LogSerchService logSerchService) {
		this.logSerchService = logSerchService;
	}

}
